package com.masterclient.app.businessmodule.minemodule.index.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.intercept.FCBaseIntercept;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FrozenDialog;
import com.masterclient.app.modules.coupon.CouponActivity;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.businessmodule.minemodule.point.PointActivity;

public class PersonDataView extends FrameLayout implements View.OnClickListener {

    //收益
    private LinearLayout llCash;
    private TextView tvCash;
    //向日葵币
    private LinearLayout llRedpack;
    private TextView tvRedpackMoney;
    //优惠券
    private View llYhq;
    private TextView tvYhqNo;
    private ImageView ivCouponValidity;
    //金币
    private FrameLayout llXfk;
    private TextView tvCard;
    private ImageView ivGoldCoinValidity;


    public PersonDataView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.layout_person_data,this);
        initUI();
    }

    public PersonDataView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_person_data,this);
        initUI();
    }

    private void initUI() {
        //收益
        llCash = findViewById(R.id.ll_cash);
        llCash.setOnClickListener(this);
        tvCash = findViewById(R.id.tv_cash);
        //向日葵币
        llRedpack = findViewById(R.id.ll_redpack);
        llRedpack.setOnClickListener(this);
        tvRedpackMoney = findViewById(R.id.tv_redpack_money);
        //优惠券
        llYhq = findViewById(R.id.ll_yhq);
        llYhq.setOnClickListener(this);
        tvYhqNo = findViewById(R.id.tv_yhq_no);
        ivCouponValidity = findViewById(R.id.iv_coupon_validity);
        //金币
        llXfk = findViewById(R.id.ll_xfk);
        llXfk.setOnClickListener(this);
        tvCard = findViewById(R.id.tv_card);
        ivGoldCoinValidity = findViewById(R.id.iv_gold_coin_validity);

    }

    public void initView() {
        Utils.setDin(tvRedpackMoney, getContext());
        Utils.setDin(tvYhqNo, getContext());
        Utils.setDin(tvCard, getContext());
        Utils.setDin(tvCash, getContext());
    }

    public void initData() {
        tvRedpackMoney.setText("0.00");
        tvYhqNo.setText("0");
        tvCard.setText("0");
        tvCash.setText("0.00");
    }

    public void refreshData(CustomerOrderInfo.DataBean dataBean) {
        tvRedpackMoney.setText(dataBean.getBupiao());
        tvYhqNo.setText(dataBean.getSalesrule_num());
        ivCouponValidity.setVisibility(dataBean.isIs_coupon_expire_flag()?VISIBLE:GONE);
        ivGoldCoinValidity.setVisibility(dataBean.isGold_expire_flag()?VISIBLE:GONE);
        tvCash.setText(dataBean.getBalance());
        tvCard.setText(dataBean.getIntegral());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_cash) {
            //收益
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                ARouter.getInstance().build(FCRouterPath.MINE_INCOME_PATH).navigation(getContext(),new FCBaseIntercept());
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_redpack) {
            //向日葵币
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                ARouter.getInstance().build(FCRouterPath.MINE_GZB_PATH).navigation(getContext(),new FCBaseIntercept());
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_yhq) {
            //优惠券
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {

                startActivitryWithBundle(CouponActivity.class, null);
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        } else if (v.getId() == R.id.ll_xfk) {
            //金币
            if ("2".equals(Config.FROZEN)) {
                FrozenDialog.showFrozen(getContext());
                return;
            }
            if (UserDataManager.getInstance().isSystemLogin()) {
                getContext().startActivity(new Intent(getContext(), PointActivity.class));
            } else {
                startActivitryWithBundle(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
            }
        }
    }


    private void startActivitryWithBundle(@NonNull Class clazz, String[][] val) {
        Intent intent = new Intent(getContext(), clazz);
        Bundle bundle = new Bundle();
        if (val != null) {
            for (String[] aVal : val) {
                bundle.putString(aVal[0], aVal[1]);
            }
            intent.putExtras(bundle);
        }
        getContext().startActivity(intent);
    }
}
